package com.example.statistics.ui.purchase

import android.os.Bundle
import android.view.View
import androidx.core.view.marginTop
import com.example.statistics.R
import com.example.statistics.bean.ColdStorageBean
import com.example.statistics.bean.ShippingInfo
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.*
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.jhj.httplibrary.model.HttpParams
import kotlinx.android.synthetic.main.activity_port_info_edit.*
import org.jetbrains.anko.dip

class PortInfoEditActivity : BaseActivity() {

    var id = 0

    override val title: String
        get() = "船务信息"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_port_info_edit)
        val purchaseProductId = intent.getIntExtra(Constants.PURCHASE_PRODUCT_ID, 0)
        val containerNo = intent.getStringExtra(Constants.CONTAINER_NO)
        val agentId = intent.getIntExtra(Constants.AGENT_ID, 0).orEmpty() //agentId == 27,现货
        if (agentId == 27) {
            tv_ship.visibility = View.GONE
            tv_port.visibility = View.GONE
        }
        if (purchaseProductId != 0) {
            httpGet<ShippingInfo>(UrlConfig.PURCHASE_PORT_DATA, config = {
                listParams = listOf("purchaseProductId" to purchaseProductId)
            }) {
                id = it?.ID ?: 0
                tv_ship.contentText = it?.shipCompany.orEmpty()
                tv_port.contentText = it?.port.orEmpty()
                tv_storage.contentText = it?.coldStorage.orEmpty()
                tv_start_date.contentText = it?.startDate.goTimeFormat().orEmpty()
                tv_arrival_date.contentText = it?.arrivalDate.goTimeFormat().orEmpty()
                tv_warehousing_date.contentText = it?.warehousingDate.goTimeFormat().orEmpty()
                tv_delivery_date.contentText = it?.deliveryDate.goTimeFormat().orEmpty()
            }
        }

        tv_ship.onContentClicked {
            singleDialog("ONE", "CMA", "YML",  "HPL","OOCL", "COSCO", "MAERSK", "YANGMING", ) { s ->
                tv_ship.contentText = s
            }
        }
        tv_port.onContentClicked {
            singleDialog("上海", "天津", "盐田") { s ->
                tv_port.contentText = s
            }
        }
        tv_storage.onContentClicked {
            httpGet<List<ColdStorageBean>>(UrlConfig.PURCHASE_STORAGE_LIST, config = {
                listParams = listOf("city" to tv_port.contentText)
            }) {
                singleDialog(*it.orEmpty().toTypedArray()) { storage ->
                    tv_storage.contentText = storage.itemName
                }
            }
        }

        topBarRightText("保存") {
            val url = if (id == 0) UrlConfig.PURCHASE_PORT_ADD else UrlConfig.PURCHASE_PORT_UPDATE
            val params = HttpParams(
                "ID" to id,
                "purchaseProductId" to purchaseProductId,
                "containerNo" to containerNo,
                "shipCompany" to tv_ship.contentText,
                "port" to tv_port.contentText,
                "coldStorage" to tv_storage.contentText,
                "startDate" to tv_start_date.contentText,
                "arrivalDate" to tv_arrival_date.contentText,
                "warehousingDate" to tv_warehousing_date.contentText,
                "deliveryDate" to tv_delivery_date.contentText
            )
            httpPost<ShippingInfo>(url, config = {
                httpParams = params
            }) {
                activityFinish()
            }
        }
    }
}